package org.komarichin.services;

import org.komarichin.beans.ILanguage;
import org.komarichin.beans.INews;
import org.komarichin.beans.dao.INewsDAO;
import org.komarichin.beans.service.INewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * User: Michael Komarichin
 * Date: 14.10.2009
 * Time: 12:42:29
 */
@Service
public class NewsService<PK> implements INewsService<PK> {

    private static final long serialVersionUID = 265682655625647326L;
    private static final Logger log = LoggerFactory.getLogger(NewsService.class);

    public NewsService() {
        log.debug("instance of news service created.");
    }

    @Autowired
    private INewsDAO<INews<PK>, PK> newsDAO;

    @Override
    public INews<PK> save(INews<PK> news) {
        return newsDAO.save(news);
    }

    @Override
    public void saveOrUpdate(INews<PK> news) {
        newsDAO.saveOrUpdate(news);
    }

    @Override
    public void remove(INews<PK> news) {
        remove(news.getId());
    }

    @Override
    public void remove(PK id) {
        newsDAO.remove(id);
    }

    @Override
    public INews<PK> findById(PK id) {
        return newsDAO.findById(id);
    }

    @Override
    public Collection<INews<PK>> getAll() {
        return newsDAO.getAll();
    }

    @Override
    public Collection<INews<PK>> getAll(ILanguage lang) {
        return newsDAO.getAll(lang);
    }

    @Override
    public Collection<INews<PK>> getNews(int start, int limit, String fieldName, boolean order) {
        return newsDAO.getEntityByParams(start, limit, fieldName, order);
    }

    @Override
    public Collection<INews<PK>> getNews(ILanguage lang, int start, int limit, String fieldName, boolean order) {
        return newsDAO.getEntityByParams(start, limit, fieldName, order, lang);
    }

    @Override
    public int getCountNews() {
        return newsDAO.getCountEntities();
    }

    @Override
    public int getCountNews(ILanguage lang) {
        return newsDAO.getCountEntities(lang);
    }

    @Override
    public Collection<INews<PK>> getLatestNews() {
        log.debug("get latest news");
        return newsDAO.getEntityByParams(0, 10, "create", false);
    }

    @Override
    public Collection<INews<PK>> getLatestNews(ILanguage lang) {
        log.debug("get latest news");
        if (lang == null) return newsDAO.getEntityByParams(0, 10, "create", false);
        return newsDAO.getEntityByParams(0, 10, "create", false, lang);
    }

}
